package com.martinwj.controller.reply;

import com.martinwj.constant.ErrorMsg;
import com.martinwj.entity.Reply;
import com.martinwj.entity.Result;
import com.martinwj.entity.User;
import com.martinwj.exception.SysException;
import com.martinwj.service.ReplyService;
import com.martinwj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName: ReplyAction
 * @Description: TODO
 * @author: martin-wj
 * @createDate: 2021-04-05
 */
@Controller
@RequestMapping("portal/reply_info")
public class ReplyAction {
    @Autowired
    private ReplyService replyService;
    @Autowired
    private UserService userService;

    /**
     * 保存用户回复
     * @param videoId 视频主键
     * @param commentId 评论主键
     * @param userToken 用户凭证
     * @param toUserId 被回复用户id
     * @param toLoginName 被回复用户登录名
     * @param content 回复内容
     * @return
     * @throws SysException
     */
    @RequestMapping("save.json")
    @ResponseBody
    public Result save(
            HttpServletRequest request,
            @RequestParam(value="videoId") String videoId,
            @RequestParam(value="commentId") String commentId,
            @RequestParam(value="userToken") String userToken,
            @RequestParam(value="toUserId") String toUserId,
            @RequestParam(value="toLoginName") String toLoginName,
            @RequestParam(value="content") String content) throws SysException {

        // 校验用户是否登录
        if (StringUtils.isEmpty(userToken)) {
            throw new SysException(ErrorMsg.ERROR_100012);
        }

        // 判断session
        HttpSession session  = request.getSession();
        // 从session中取出用户身份信息
        User userInfo = (User)session.getAttribute("userInfo");

        if (userInfo==null) {
            userInfo = userService.getUserInfoByUserToken(userToken);
            // 将用户信息保存进session
            request.getSession().setAttribute("userInfo", userInfo);
        }

        // 校验是否是自己回复自己
        if (toUserId.equals(userInfo.getId())) {
            throw new SysException(ErrorMsg.ERROR_900002);
        }

        // 校验回复内容
        if (StringUtils.isEmpty(content)) {
            throw new SysException(ErrorMsg.ERROR_900001);
        }

        // 删除普通标签
        content = content.replaceAll("<(S*?)[^>]*>.*?|<.*? />", "");
        // 删除转义字符
        content = content.replaceAll("&.{2,6}?;", "");

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = formatter.format(currentTime);

        // 保存回复内容
        Reply replyInfo = new Reply();
        replyInfo.setVideoId(videoId);
        replyInfo.setCommentId(commentId);
        replyInfo.setToUserId(toUserId);
        replyInfo.setToLoginName(toLoginName);
        replyInfo.setContent(content);
        replyInfo.setUserId(userInfo.getId());
        replyInfo.setLoginName(userInfo.getLoginName());
        replyInfo.setUpdateTime(now);

        replyService.save(replyInfo);

        return Result.success();
    }
}
